package com.spring.boot.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 时间工具类
 */
public class DateUtil {

    public static final String FULL_TIME_PATTERN = "yyyyMMddHHmmss";

    public static final String FULL_TIME_SPLIT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static final String format_date_day = "yyyy-MM-dd HH:mm";//天  2019-09-15 00:00
    public static final String format_date_week = "yyyy-MM";//周
    public static final String format_date_month = "yyyy-MM";//月
    public static final String format_date_half_a_year = "yyyy-MM";//半年
    public static final String format_date_year = "yyyy";//年

    public static final String DATE_TIME_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";

    public static final DateTimeFormatter dtfHms = DateTimeFormatter.ofPattern(FULL_TIME_SPLIT_PATTERN);

    public static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT_YYYY_MM_DD);

    public static String formatFullTime(LocalDateTime localDateTime) {
        return formatFullTime(localDateTime, FULL_TIME_PATTERN);
    }

    public static String formatFullTime(LocalDateTime localDateTime, String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(dateTimeFormatter);
    }

    public static String getDateFormat(Date date, String dateFormatType) {
        SimpleDateFormat simformat = new SimpleDateFormat(dateFormatType);
        if(date==null){
            date = new Date();
        }
        return simformat.format(date);
    }

    public static String formatCSTTime(String date, String format) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        Date d = sdf.parse(date);
        return DateUtil.getDateFormat(d, format);
    }


    /**
     * 获得当前时间  liangtengfei   年月日时分秒
     * @return
     */
    public static String getCurrentTime(){
        LocalDateTime  now = LocalDateTime.now();
        String format = dtfHms.format(now);
        return format;
    }

    /**
     * 获得当前时间  liangtengfei  年月日
     * @return
     */
    public static String getCurrentDay(){
        LocalDate now = LocalDate.now();
        String format = dtf.format(now);
        return format;
    }



    public static String getDateFormat(Integer dateType, String format) throws ParseException {
        String date = null;
        //format_date_day
        //0：天、1：周 、2：月、 3：半年、 4：一年
        if(dateType.equals(0)){
            SimpleDateFormat sdf = new SimpleDateFormat(format_date_day);
            date = sdf.format(sdf.parse(format));
        }else if(dateType.equals(1)){
            SimpleDateFormat sdf = new SimpleDateFormat(format_date_week);
            date = sdf.format(sdf.parse(format));
        }else if(dateType.equals(2)){
            SimpleDateFormat sdf = new SimpleDateFormat(format_date_month);
            date = sdf.format(sdf.parse(format));
        }else if(dateType.equals(3)){
            SimpleDateFormat sdf = new SimpleDateFormat(format_date_half_a_year);
            date = sdf.format(sdf.parse(format));
        }else if(dateType.equals(4)){
            SimpleDateFormat sdf = new SimpleDateFormat(format_date_year);
            date = sdf.format(sdf.parse(format));
        }
        return date;
    }

    public static String getLastDayOfMonth(String date) {
        int year = Integer.parseInt(date.split("-")[0]);
        int month = Integer.parseInt(date.split("-")[1]);
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置月份
        cal.set(Calendar.MONTH, month); //设置当前月的上一个月
        // 获取某月最大天数
        //int lastDay = cal.getActualMaximum(Calendar.DATE);
        int lastDay = cal.getMinimum(Calendar.DATE); //获取月份中的最小值，即第一天
        // 设置日历中月份的最大天数
        //cal.set(Calendar.DAY_OF_MONTH, lastDay);
        cal.set(Calendar.DAY_OF_MONTH, lastDay - 1); //上月的第一天减去1就是当月的最后一天
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }

    public static String startTimeVdoing(Integer dateType){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dNow = new Date();
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        String startTime = null;
        switch (dateType){
            case 0://日
                calendar.add(Calendar.DATE, -30);
                dBefore = calendar.getTime();
                startTime = sdf.format(dBefore);
                break;
            case 1://周
                calendar.add(Calendar.MONTH, -3);
                dBefore = calendar.getTime();
                startTime = sdf.format(dBefore);
                break;
            case 2://月
//                calendar.add(Calendar.MONTH, -12);
                calendar.add(Calendar.MONTH, -6);// update by ltf
                dBefore = calendar.getTime();
//                //  获取当月第一天  add by ltf  updte
//                calendar.add(Calendar.MONTH, 0);
//                calendar.set(Calendar.DAY_OF_MONTH, 1);
//                calendar.set(Calendar.HOUR_OF_DAY,0);
//                calendar.set(Calendar.MINUTE, 0);
//                calendar.set(Calendar.SECOND, 0);
                //  获取当月最后一天  add by ltf  updte
                calendar.set(Calendar.DAY_OF_MONTH, 0);
                calendar.set(Calendar.HOUR_OF_DAY,23);
                calendar.set(Calendar.MINUTE, 59);
                calendar.set(Calendar.SECOND, 59);
                startTime = sdf.format(calendar.getTime());
                // end add
                break;
            case 3://半年
                calendar.add(Calendar.MONTH, -36);
                dBefore = calendar.getTime();
                startTime = sdf.format(dBefore);
                break;
            case 4://年
                calendar.add(Calendar.YEAR, -5);
                dBefore = calendar.getTime();
                startTime = sdf.format(dBefore);
                break;
        }
        return startTime;
    }

    /**
     * 判断是否是闰年
     */
    public static boolean isLeapYear(int year){
        if (year % 100 == 0 && year % 400 == 0){
            return true;
        }else if (year % 100 != 0 && year % 4==0){
            return true;
        }
        return false;
    }

    /**
     * 得到某月有多少天
     * 哈哈少儿口诀：1357810腊三十一天永不差，469冬三十日平年二月28，闰年再把一天加。
     */
    public static int getDaysOfMonth(boolean isLeapYear,int month){
        int days=0;
        switch (month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days=31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days=30;
                break;
            case 2:
                if (isLeapYear){
                    days=29;
                }else{
                    days=28;
                }
        }
        return days;
    }

    public static String stopTimeVdoing(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前月的 上个月  update by ltf
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return sdf.format(calendar.getTime());
    }

    public static String stopTimeVdoing(String endTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(StringUtils.isEmpty(endTime)){

            // 获取当前月的 上个月  update by ltf
            Calendar calendar = Calendar.getInstance(); //得到日历
            calendar.set(Calendar.DAY_OF_MONTH, 0);
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            return sdf.format(calendar.getTime());
        }else{
            try {
                endTime = endTime + " 23:59:59";
                Date date = sdf.parse(endTime);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(Calendar.MONTH,1);
                calendar.set(Calendar.DAY_OF_MONTH, 0);
                calendar.set(Calendar.HOUR_OF_DAY,23);
                calendar.set(Calendar.MINUTE, 59);
                calendar.set(Calendar.SECOND, 59);
                return sdf.format(calendar.getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return "";
    }


    public static String caculatestartTime(String startTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            // 获取当前月的 上个月  update by ltf
        try {
            startTime = startTime + " 00:00:00";
            Date date = sdf.parse(startTime);
            Calendar calendar = Calendar.getInstance(); //得到日历
            calendar.setTime(date);
            calendar.set(Calendar.DAY_OF_MONTH, 0);
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            return sdf.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }


    public static String startTimeVdoingNew(Integer dateType,String endTime){

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(endTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);

            calendar.add(Calendar.MONTH, -5);// update by ltf
            calendar.set(Calendar.DAY_OF_MONTH, 0);
            calendar.set(Calendar.HOUR_OF_DAY,23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            String startTime = sdf.format(calendar.getTime());

            return startTime;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }


    public static String caculateStartTime(Integer num,String endTime){

        try {
            num = num -1;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(endTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);

            calendar.add(Calendar.MONTH, -num);// update by ltf
            calendar.set(Calendar.DAY_OF_MONTH, 0);
            calendar.set(Calendar.HOUR_OF_DAY,23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            String startTime = sdf.format(calendar.getTime());

            return startTime;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }


    public static String changeDateFormat(String endTime){

        try {
            if(StringUtils.isEmpty(endTime)){
                return endTime;
            }
            // 转换格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
            Date parse = sdf.parse(endTime);
            sdf = new SimpleDateFormat("yyyy-MM-dd");
            endTime = sdf.format(parse);

            return endTime;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return endTime;
    }

    public static List<String> createResultList(String endTime) {
        List<String> list = new ArrayList<>();
        if(StringUtils.isEmpty(endTime)){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            // 当前月的上个月
            Calendar calendar = Calendar.getInstance(); //得到日历
            calendar.set(Calendar.DAY_OF_MONTH, 0);
            Date time = calendar.getTime();
            String format = sdf.format(time);
            list.add(format);
            for (int i = 6; i > 1; i--) {
                calendar.add(Calendar.MONTH, -1);
                Date time1 = calendar.getTime();
                format = sdf.format(time1);
                list.add(format);
            }
            return list;
        }else{
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                Date date = sdf.parse(endTime);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                Date time = calendar.getTime();
                String format = sdf.format(time);
                list.add(format);
                for (int i = 6; i > 1; i--) {
                    calendar.add(Calendar.MONTH, -1);
                    Date time1 = calendar.getTime();
                    format = sdf.format(time1);
                    list.add(format);
                }
                return list;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return list;
    }


    public static List<String> createNewResultList(Integer num,String endTime) {
        List<String> list = new ArrayList<>();
        if(StringUtils.isEmpty(endTime)){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            // 当前月的上个月
            Calendar calendar = Calendar.getInstance(); //得到日历
            calendar.set(Calendar.DAY_OF_MONTH, 0);
            Date time = calendar.getTime();
            String format = sdf.format(time);
            list.add(format);
            for (int i = num; i > 1; i--) {
                calendar.add(Calendar.MONTH, -1);
                Date time1 = calendar.getTime();
                format = sdf.format(time1);
                list.add(format);
            }
            return list;
        }else{
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                Date date = sdf.parse(endTime);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                Date time = calendar.getTime();
                String format = sdf.format(time);
                list.add(format);
                for (int i = num; i > 1; i--) {
                    calendar.add(Calendar.MONTH, -1);
                    Date time1 = calendar.getTime();
                    format = sdf.format(time1);
                    list.add(format);
                }
                return list;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return list;
    }



    public static List<Map<String,String>> createListforAllYear(String endTime) {


        List<String> list = new ArrayList<>();
        if(StringUtils.isEmpty(endTime)){// 目前一定有值 所以此处先不改  -- todo 2020 -01-22

        }else{
            try {
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                // 当前月的上个月
                String strFlag = endTime.substring(5,7);// 格式 2020 -01-22
                Integer integer = Integer.valueOf(strFlag);
                Integer flag = 1;
                if(integer <= 6){
                    // 上半年
                    endTime = endTime.substring(0,5);
                    endTime = endTime + "06" + "-30 23:59:59";
                    flag = 1;
                }else{
                    // 下半年
                    // 上半年
                    endTime = endTime.substring(0,5);
                    endTime = endTime + "12" + "-31 23:59:59";
                    flag = 2;

                }
                Date date = sdf1.parse(endTime);
//
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                list.add(endTime);
                for (int i = 4; i > 1; i--) {
                    if(flag == 2){// 下半年  没问题 ok

                        calendar.add(Calendar.MONTH, -12);


                        Date time1 = calendar.getTime();
                        endTime = sdf1.format(time1);
                        list.add(endTime);
                    }
                    if(flag == 1){ //上半年

                        if(i == 4){
                            calendar.add(Calendar.MONTH, -6);
                            Date time1 = calendar.getTime();
                            endTime = sdf1.format(time1);
                            endTime = endTime.substring(0,8) + "31 23:59:59";
                            list.add(endTime);
                        }else{
                            calendar.add(Calendar.MONTH, -12);
                            Date time1 = calendar.getTime();
                            endTime = sdf1.format(time1);
                            endTime = endTime.substring(0,8) + "31 23:59:59";
                            list.add(endTime);
                        }
                    }
                }
                Collections.reverse(list);


//        // 组装 list<map>
                List<Map<String,String>> timelist = new ArrayList<>();
                int size = list.size();
//        size = size/2;
                for(int i = 0;i<size-1;i++){

                    Map<String,String> map = new HashMap<>();
                    map.put("startTime",list.get(i));
                    map.put("endTime",list.get(i+1));
                    timelist.add(map);
                }
                return timelist;

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return null;
    }




    public static List<Map<String,String>> createListforHalfYear(String endTime) {


        List<String> list = new ArrayList<>();
        if(StringUtils.isEmpty(endTime)){// 目前一定有值 所以此处先不改  -- todo 2020 -01-22

        }else{
            try {
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                // 当前月的上个月
                String strFlag = endTime.substring(5,7);// endtime 格式：2020 -01-22
                Integer integer = Integer.valueOf(strFlag);
                Integer flag = 1;
                if(integer <= 6){
                    // 上半年
                    endTime = endTime.substring(0,5);
                    endTime = endTime + "06" + "-30 23:59:59";
                    flag = 1;
                }else{
                    // 下半年
                    // 上半年
                    endTime = endTime.substring(0,5);
                    endTime = endTime + "12" + "-31 23:59:59";
                    flag = 2;

                }
                Date date = sdf1.parse(endTime);
//
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                list.add(endTime);
                for (int i = 4; i > 1; i--) {
                    if(flag == 2){// 下半年  没问题 ok

                        calendar.add(Calendar.MONTH, -12);

                        Calendar calendar1 = (Calendar)calendar.clone();
                        calendar1.add(Calendar.MONTH, 6);
                        Date time2 = calendar1.getTime();
                        endTime = sdf1.format(time2);
                        list.add(endTime);

                        Date time1 = calendar.getTime();
                        endTime = sdf1.format(time1);
                        list.add(endTime);
                    }
                    if(flag == 1){ //上半年

                        calendar.add(Calendar.MONTH, -12);
                        Calendar calendar1 = (Calendar)calendar.clone();
                        calendar1.add(Calendar.MONTH, 6);
                        Date time2 = calendar1.getTime();
                        endTime = sdf1.format(time2);
                        endTime = endTime.substring(0,8) + "31 23:59:59";
                        list.add(endTime);

                        Date time1 = calendar.getTime();
                        endTime = sdf1.format(time1);
                        list.add(endTime);
                    }
                }
                Collections.reverse(list);


//        // 组装 list<map>
                List<Map<String,String>> timelist = new ArrayList<>();
                int size = list.size();
//        size = size/2;
                for(int i = 0;i<size-1;i++){

                    Map<String,String> map = new HashMap<>();
                    map.put("startTime",list.get(i));
                    map.put("endTime",list.get(i+1));
                    timelist.add(map);
                }
                return timelist;

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static int calulateMonth(String startTime,String endTime){

        try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();

        bef.setTime(sdf.parse(endTime));
        aft.setTime(sdf.parse(startTime));
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
            Integer abs = Math.abs(month + result);
            return abs;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }



    // 系统管理 宽带用户 流量，活跃链接  设置时间格式
    public static String changeDateForAdmin(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyMMdd");
        Date parse = null;
        try {
            parse = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        time = sdf1.format(parse);
        return time;
    }


    public static String changeUploadTimeForAdmin(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        Date parse = null;
        try {
            parse = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        time = sdf1.format(parse);
        return time;
    }

    // 系统管理 宽带用户 流量，活跃链接  设置时间格式
    public static String changeDateForWebPage(String time) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        Date parse = null;
        try {
            parse = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        time = sdf1.format(parse);
        return time;
    }

    public static String changeUploadTimeForWebPage(String time) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date parse = null;
        try {
            parse = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        time = sdf1.format(parse);
        return time;
    }

    /**
     * 获取固定时间段内的日期集合，格式为yyyy-MM-dd格式
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<String> getDayList(Date startDate,Date endDate){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        List<String> dateList = new ArrayList<>();

        if(startDate.after(endDate)){
            return dateList;
        }

        while(startDate.before(endDate)){
           dateList.add(format.format(startDate));

           Calendar calendar = Calendar.getInstance();
           calendar.setTime(startDate);
           calendar.add(Calendar.DATE,1);

           startDate = calendar.getTime();

        }
        dateList.add(format.format(endDate));

        return dateList;
    }


    /**
     * 获取某个时间段list，按天统计，格式为yyyyMMdd
     *
     */
    public static List<String> getDayList(String cntDateBeg, String cntDateEnd) throws Exception{
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        format.parse(cntDateBeg);
        format.parse(cntDateEnd);
        List<String> list = new ArrayList<>();
        //拆分成数组
        String[] dateBegs = cntDateBeg.split("-");
        String[] dateEnds = cntDateEnd.split("-");
        //开始时间转换成时间戳
        Calendar start = Calendar.getInstance();
        start.set(Integer.valueOf(dateBegs[0]), Integer.valueOf(dateBegs[1]) - 1, Integer.valueOf(dateBegs[2]));
        Long startTIme = start.getTimeInMillis();
        //结束时间转换成时间戳
        Calendar end = Calendar.getInstance();
        end.set(Integer.valueOf(dateEnds[0]), Integer.valueOf(dateEnds[1]) - 1, Integer.valueOf(dateEnds[2]));
        Long endTime = end.getTimeInMillis();
        //定义一个一天的时间戳时长
        Long oneDay = 1000 * 60 * 60 * 24l;
        Long time = startTIme;
        //循环得出
        while (time <= endTime) {
            list.add(new SimpleDateFormat("yyyy-MM-dd").format(new Date(time)));
            time += oneDay;
        }
        return list;
    }


    /**
     * 获取两个日期相差的月数
     */
    public static int getMonthDiff(Date d1, Date d2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(d1);
        c2.setTime(d2);
        int year1 = c1.get(Calendar.YEAR);
        int year2 = c2.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH);
        int month2 = c2.get(Calendar.MONTH);
        int day1 = c1.get(Calendar.DAY_OF_MONTH);
        int day2 = c2.get(Calendar.DAY_OF_MONTH);
        // 获取年的差值 
        int yearInterval = year1 - year2;
        // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
        if (month1 < month2 || month1 == month2 && day1 < day2) {
            yearInterval--;
        }
        // 获取月数差值
        int monthInterval = (month1 + 12) - month2;
        if (day1 < day2) {
            monthInterval--;
        }
        monthInterval %= 12;
        int monthsDiff = Math.abs(yearInterval * 12 + monthInterval);
        return monthsDiff;

    }

    /**
     * 判断该日期是否是该月的最后一天
     *
     * @param date
     *            需要判断的日期
     * @return
     */
    public static boolean isLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH) == calendar
                .getActualMaximum(Calendar.DAY_OF_MONTH);
    }


    public static void main(String[] args) throws Exception{
//        List<Map<String, String>> listforHalfYear = createListforHalfYear("200122");
//        for(Map<String, String> map : listforHalfYear){
//
//            System.out.println(map);
//        }
//        String s = startTimeVdoing(2);
//        System.out.println("近半年起始时间：" +s);
//        String s1 = stopTimeVdoing();
//        System.out.println("近半年终止时间：" +s1);
// int num = 12;
//        num= num/2 + 1;
//        System.out.println(num);
//        String s2 = caculatestartTime("2019-01-01");
//        System.out.println(s2);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List<String> dayList = getDayList(format.parse("2020-03-28"),format.parse("2020-04-02"));

        System.out.println(dayList);
    }

}
